<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 2018/1/21
 * Time: 18:59
 */
namespace backend\controllers;

use common\models\ar\RealName;
use common\models\ar\User;
use common\models\ar\UserBrokers;
use common\models\ar\Images;
use common\models\RealNameDao;
use common\models\UserInfoDetailDao;
class RealNameCheckController extends BackendController
{
    public $enableCsrfValidation = false;
    /**
     * 提交实名的记录
     */
    public function actionIndex()
    {

        $req = \Yii::$app->request;
        $isRender = $req->get('isRender', 1);//1为搜索页面，2为异步获取
        if ($isRender == 1) {
            return $this->renderPartial('lists');
        }

        $status = $req->get('update_status', -1);
        $clientSN = trim($req->get('clientSN', ''));
        $uid = -1;

        if ($clientSN != '') {
            if (is_numeric($clientSN)) {

                //数字类型，以客户编号搜索
                if (UserBrokers::findOne(['loginName'=>$clientSN])){
                    $tmp=UserBrokers::findOne(['loginName'=>$clientSN]);
                    $uid=$tmp->loginName;
                }else if(User::findOne(['loginName'=>$clientSN])){
                    $tmp=User::findOne(['loginName'=>$clientSN]);
                    $uid=$tmp->loginName;
                }else{
                    $uid=0;
                }
            }else {
                //非数字类型，以姓名搜索
                $uid=0;
            }
        }
        $lists = RealNameDao::lists($uid, $status);
        exit(json_encode($lists,true));
    }

    public function actionDetail()
    {
        $uid = (int)\Yii::$app->request->get('userID', 0);
        $level=RealName::findOne(['userID'=>$uid]);
        if ($level['level']==1){
            $obj = User::findOne(['userID'=>$uid]);
        }else{
            $obj=UserBrokers::findOne(['id'=>$uid]);
        }
        if (!$obj) {
            exit("用户不存在");
        }
        $fields = UserInfoDetailDao::info($uid, 6, 1);
        return $this->renderPartial('detail', [
            'uid' => $uid,
            'fields' => $fields,
            'record' => RealName::findOne($uid),
        ]);
    }

    public function actionOperate()
    {
        $uid = (int)\Yii::$app->request->get('userID', 0);//用户ID
        $tmp = RealName::findOne($uid);

        if (!$tmp || $tmp->update_status != 1) {
            exit(json_encode(['code' => 0, 'msg' => '状态已变化']));
        }

        $type = (int)\Yii::$app->request->get('type', 0);//0拒绝，1通过 9永久性拒绝
        $errorMsg = \Yii::$app->request->get('error_msg', '');//拒绝原因

        $bool = RealNameDao::operate($uid, $type, $errorMsg);
        if ($bool) {
            $code = 1;
            $msg = '操作成功';
        } else {
            $code = 0;
            $msg = '操作失败';
        }

        exit(json_encode(['code' => $code, 'msg' => $msg]));
    }

    /**
     实名信息修改
     **/
    public function actionModify()
    {
        $req=\Yii::$app->request;
        $uid = $req->get('userID');
        $data=$req->get('data');
        $data = trim($data,',');
        $data = explode(',',$data);
        $j=0;
        //修改实名信息
        $db = \Yii::$app->db;
        $sql="select field_id,field_value from `user_info_detail` where userID=:uid and field_id not in (73,75,76,83)";
        $uids[':uid'] = $uid;
        $names=$db->createCommand($sql,$uids)->queryAll();
        for ($i=0;$i<count($data);$i++){

            if ($names[$i]['field_value']==$data[$i]){
                $j++;
            }else{
                $sql="UPDATE `user_info_detail` SET field_value='".$data[$i]."' WHERE userID=".$uid." AND field_id =".$names[$i]['field_id'];
//                $uids[':f_value'] = $data[$i];
//                $uids[':field_id'] = $names[$i]['field_id'];
                $db->createCommand($sql)->execute();
            }
        }
        //通过$j的值来判断多少处修改
        if ($j==7){
            $code = 0;
            $msg = '您暂时没有修改实名信息内容！';
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }else{
            $code = 1;
            $msg = "实名信息修改成功！";
            exit(json_encode(['code' => $code, 'msg' => $msg]));
        }
    }

    public function actionImages(){
        $req=\Yii::$app->request;
        $uid = $req->post('userID');
        $data=$req->post('data');
        $img_s=$req->post('imgs');
        $db = \Yii::$app->db;
        $sql="SELECT field_value FROM `user_info_detail` WHERE userID=:uid and field_id=:field_id";
        $uids[':uid'] = $uid;
        $uids[':field_id'] = $data;
        $names=$db->createCommand($sql,$uids)->queryOne();
        $imgs=Images::findOne(['id'=>$names['field_value']]);
        if ($imgs->value==$imgs){
            $code = 0;
            $msg = "请图片信息未修改！";
        }else{
            $imgs->value=$img_s;
            $imgs->save(false);
            $code = 1;
            $msg = "修改成功！";
        }
        exit(json_encode(['code' => $code, 'msg' => $msg]));
    }
}